package com.shuo.dsa.sort;

import java.util.Arrays;

/**
 * <a href="http://blog.51cto.com/13733462/2113397">QuickSort</a>
 */
public class QuickSort {

    public static void quickSort(int[] arr, int left, int right) {
        int pivotIndex;
        if (left < right) {
            pivotIndex = partition(arr, left, right);
            quickSort(arr, left, pivotIndex - 1);
            quickSort(arr, pivotIndex + 1, right);
        }
    }

    private static int partition(int[] arr, int L, int R) {
        int pivot = arr[L];
        while (L < R) {
            while (L < R && arr[R] >= pivot) R--;
            arr[L] = arr[R];
            while (L < R && arr[L] <= pivot) L++;
            arr[R] = arr[L];
        }
        arr[L] = pivot;
        return L;
    }


    public static void main(String[] args) {
        int arr[] = {65, 58, 95, 10, 57, 62, 13, 106, 78, 23, 85};
        quickSort(arr, 0, arr.length - 1);
        System.out.println("排序后：" + Arrays.toString(arr));
    }

}
